package in.muton.edushare.poll.reports;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.summary.html.TableDateProvider;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.PollsDAO;
import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollQuestions;
import com.edushare.common.vo.UserPoll;
import com.edushare.common.vo.Users;

public class PollReportMainPage extends WebPage {
	public PollReportMainPage(PageParameters params) {
		add(CSSPackageResource.getHeaderContribution(PollReportMainPage.class,
				"PollReport.css"));
		init(params);

		add(new Label("pollTitle", getPoll().getPollName()));
		add(new Label("audience", ""));
		add(new Label("pollAttrib", ""));
		GridView<IModel<Users>> gv = new GridView<IModel<Users>>("respRows",
				new TableDateProvider<Users>(getPollInvitees())) {

			private static final long serialVersionUID = -6289000339911840396L;

			@Override
			protected void populateEmptyItem(Item<IModel<Users>> arg0) {
				arg0.add(new Label("respName", ""));
				arg0.add(new Label("status", ""));
			}

			@Override
			protected void populateItem(Item<IModel<Users>> arg0) {
				Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("respName", u.getCompleteUserName()));
				arg0.add(new Label("status", getPollStatus(u)));
			}
		};
		add(gv);
		gv.setColumns(3);

		add(new ListView<String>("questionList", getQuestions()) {
			private static final long serialVersionUID = 7286820944897123779L;
			@Override
			protected void populateItem(ListItem<String> arg0) {
				arg0.add(new MultiLineLabel("question",(arg0.getIndex()+1)+". "+arg0.getModelObject()));
			}
		});
		final int questionNum = getQuestions().size();
		
		add(new ListView<Integer>("questionNumLV",getIntList(questionNum)){
			private static final long serialVersionUID = 8469398186940005575L;
			@Override
			protected void populateItem(ListItem<Integer> arg0) {
				arg0.add(new Label("itemNumber",arg0.getModelObject().toString()));
			}
		});
		
		add(new ListView<IModel<Users>>("userResponsesLV", getPPLAnswered().getObject()){
			private static final long serialVersionUID = 8959010590941196630L;
			@Override
			protected void populateItem(final ListItem<IModel<Users>> arg0) {
				final Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("respName", new Model<String>(){
					private static final long serialVersionUID = -4366739661544133492L;
					@Override
					public String getObject(){
						if(getPoll().getPollAnonymous() == Poll.ANONYMOUS_POLL)
							return "Respondent "+ arg0.getIndex();
						return u.getCompleteUserName();
					}
				}));
				RepeatingView rv = new RepeatingView("userRespRowLV");
				arg0.add(rv);
				for(int i=1;i<=questionNum;i++){
					WebMarkupContainer wmc;
					rv.add(wmc = new WebMarkupContainer(rv.newChildId()));
					PollQuestions pq = new PollsDAO(getORM()).getQuestionBySNo(
							getPoll(), i);
					UserPollDAO updDao = new UserPollDAO(getORM());
					String s = updDao.getUserAnswerReport(u, pq);
					wmc.add(new Label("userRespItem", s).setEscapeModelStrings(false));
				}
			}
		});
	}

	public static PageParameters getParams(Poll poll) {
		PageParameters p = new PageParameters();
		p.add("0", poll.getPollId().toString());
		return p;
	}

	public void init(PageParameters p) {
		Integer pollId = p.getAsInteger("0", 0);
		if (pollId == 0) {
			throw new RestartResponseException(URLLoader
					.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
		}
		PollsDAO pDao = new PollsDAO(getORM());
		Poll poll = pDao.getPollById(pollId);
		if (poll == null) {
			throw new RestartResponseException(URLLoader
					.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
		}
		pollModel = new CayenneModel<Poll>(poll);
	}

	private CayenneList<Users> getPollInvitees() {
		return new CayenneList<Users>(new UserPollDAO(getORM())
				.getPollRespondents(getPoll()));
	}

	private CayenneList<Users> getPPLAnswered() {
		List<Users> allUsers = new UserPollDAO(getORM())
				.getPollRespondents(getPoll());
		UserPollDAO upDAO = new UserPollDAO(getORM());
		CayenneList<Users> ansUsers = new CayenneList<Users>(null);
		for (Users u : allUsers) {
			if (upDAO.getPollStatus(u, getPoll()) != UserPoll.POLL_PENDING) {
				ansUsers.add(u);
			}
		}
		return ansUsers;
	}

	private String getPollStatus(Users user) {
		UserPollDAO upDAO = new UserPollDAO(getORM());
		if (upDAO.getPollStatus(user, getPoll()) == UserPoll.POLL_PENDING && 
				getPoll().getPollAnonymous()!=Poll.ANONYMOUS_POLL)
			return "**";
		return "";
	}

	private ORMInterface getORM() {
		return EduShareSession.get().getSessionORMInterface();
	}

	private Poll getPoll() {
		return pollModel.getObject();
	}

	private List<Integer> getIntList(int i) {
		List<Integer> intList = new ArrayList<Integer>();
		for (int j = 1; j <= i; j++) {
			intList.add(j);
		}
		return intList;
	}

	private List<String> getQuestions() {
		PollsDAO pDAO = new PollsDAO(getORM());
		int size = getPoll().getQuestions().size();
		List<String> qList = new ArrayList<String>();
		for (int i = 1; i <= size; i++) {
			qList.add(pDAO.getQuestionBySNo(getPoll(), i).getQuestion());
		}
		return qList;
	}

	private IModel<Poll> pollModel = null;
}
